//
//  LFNavgitionBar.m
//  College
//
//  Created by LF on 2018/11/16.
//  Copyright © 2018 MuXi. All rights reserved.
//

#import "LFNavigationBar.h"

@interface LFNavigationBar ()

@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation LFNavigationBar


#pragma mark - Initializer

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    _statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, statusHeight)];
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, _statusBar.bottom, [[UIScreen mainScreen] bounds].size.width, 44)];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = UIColor.blackColor;
    _titleLabel.font = [UIFont systemFontOfSize:19.0];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [_leftButton setImage:[UIImage imageNamed:@"icon_arrow_left"] forState:UIControlStateNormal];
    [_leftButton setTitleColor:[UIColor lf_colorWithHexString:@"#A5BFCB"] forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];
    
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [_rightButton setTitleColor:[UIColor lf_colorWithHexString:@"#A5BFCB"] forState:UIControlStateNormal];
    [_rightButton addTarget:self action:@selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_statusBar];
    [self addSubview:_contentView];
    
    [self.contentView addSubview:_titleLabel];
    [self.contentView addSubview:_leftButton];
    [self.contentView addSubview:_rightButton];
    [self.contentView addBottomBorderWithHeight:0.5 andColor:UIColor.lightGrayColor];
    return self;
}
// 单标题
- (instancetype)initWithTitle:(NSString *)title {
    self = [self init];
    if (!self) {
        return nil;
    }
    
    _title = title;
    _leftButton.hidden = YES;
    
    [self refreshData];
    
    return self;
}

// 右字
- (instancetype)initWithTitle:(NSString *)title rightTitle:(NSString *)rightTitle rightAction:(Action)rightAction {
    self = [self init];
    if (!self) {
        return nil;
    }
    
    _title = title;
    _rightTitle = rightTitle;
    _rightAction = rightAction;
    
    [self refreshData];
    
    return self;
}
- (instancetype)initWithTitle:(NSString *)title rightTitle:(NSString *)rightTitle leftAction:(Action)leftAction rightAction:(Action)rightAction {
    self = [self init];
    if (!self) {
        return nil;
    }
    
    _title = title;
    _rightTitle = rightTitle;
    _leftAction = leftAction;
    _rightAction = rightAction;
    
    [self refreshData];
    
    return self;
}
// 左图  右字
- (instancetype)initWithTitle:(NSString *)title leftImage:(UIImage *)leftImage rightTitle:(NSString *)rightTitle leftAction:(Action)leftAction rightAction:(Action)rightAction {
    self = [self init];
    if (!self) {
        return nil;
    }
    
    _title = title;
    _leftImage = leftImage;
    _rightTitle = rightTitle;
    _leftAction = leftAction;
    _rightAction = rightAction;
    
    [self refreshData];
    return self;
}
// 左图  右图
- (instancetype)initWithTitle:(NSString *)title leftImage:(UIImage *)leftImage rightImage:(UIImage *)rightImage leftAction:(Action)leftAction  rightAction:(Action)rightAction {
    self = [self init];
    if (!self) {
        return nil;
    }
    
    _title = title;
    _leftImage = leftImage;
    _rightImage = rightImage;
    _leftAction = leftAction;
    _rightAction = rightAction;
    
    [self refreshData];
    return self;
}
// 左图
- (instancetype)initWithTitle:(NSString *)title leftImage:(UIImage *)leftImage leftAction:(Action)leftAction {
    self = [self init];
    if (!self) {
        return nil;
    }
    
    _title = title;
    _leftImage = leftImage;
    _leftAction = leftAction;
    
    [self refreshData];
    return self;
}

#pragma mark - refresh
- (void) refreshData {
    
    if (_bgColor) {
        self.backgroundColor = _bgColor;
    } else {
        self.backgroundColor = UIColor.whiteColor;
    }
    
    _titleLabel.text = _title;
    
    if (_leftImage) {
        [_leftButton setTitle:@"" forState:UIControlStateNormal];
        [_leftButton setImage:_leftImage forState:UIControlStateNormal];
//        [_leftButton setBackgroundImage:_leftImage forState:UIControlStateNormal];
    }
    
    if (_leftTitle) {
        [_leftButton setTitle:_leftTitle forState:UIControlStateNormal];
        [_leftButton setImage:nil forState:UIControlStateNormal];
//        [_leftButton setBackgroundImage:nil forState:UIControlStateNormal];
    }
    
    if (_rightImage) {
        [_rightButton setTitle:@"" forState:UIControlStateNormal];
        [_rightButton setImage:_rightImage forState:UIControlStateNormal];
//        [_rightButton setBackgroundImage:_rightImage forState:UIControlStateNormal];
    }
    
    if (_rightTitle) {
        [_rightButton setTitle:_rightTitle forState:UIControlStateNormal];
        [_rightButton setImage:nil forState:UIControlStateNormal];
//        [_rightButton setBackgroundImage:nil forState:UIControlStateNormal];
    }
    
//    [self lf_setRandomColorForSbView];
//    [self.contentView lf_setRandomColorForSbView];
    
    [self updateConstraintsIfNeeded];
    [self setNeedsUpdateConstraints];
}

#pragma mark - Layout
- (void)updateConstraints {
    
    UIView *contentView = self.contentView;
    
    [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(contentView);
    }];
    
    [_leftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        if (self.leftImage) {
            make.size.mas_equalTo(CGSizeMake(KImgWH, KImgWH));
        } else {
            make.size.mas_equalTo(self.leftSize.width > 0 ? self.leftSize : CGSizeMake(KImgWH, KImgWH));
        }
        make.left.equalTo(contentView).with.offset(self.marginLeft > 0 ? self.marginLeft : 5);
    }];
    
    [_rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        make.right.equalTo(contentView).with.offset(self.marginRight > 0 ? -self.marginRight : -5);
        if (self.rightImage) {
            make.size.mas_equalTo(CGSizeMake(KImgWH, KImgWH));
        } else {
            make.size.mas_equalTo(self.rightSize.width > 0 ? self.rightSize : CGSizeMake((self.rightButtonLength > 0) ? self.rightButtonLength : 60, 24));
        }
    }];
    [super updateConstraints];
}

#pragma mark - Action
- (void)didTouchButton:(UIButton *)sender {
    if (sender == _leftButton && self.leftAction) {
        if (self.leftAction) {
            self.leftAction();
        } else {
            [CURRENT_NAV popViewControllerAnimated:YES];
        }
    } else if (sender == _rightButton && self.rightAction) {
        self.rightAction();
    }
}

#pragma mark - Setter
- (void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    [self refreshData];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    [self refreshData];
}

- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    _leftImage = nil;
    [self refreshData];
}

- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    _rightImage = nil;
    [self refreshData];
}

- (void)setLeftImage:(UIImage *)leftImage {
    _leftImage = leftImage;
    _leftTitle = nil;
    [self refreshData];
}

- (void)setRightImage:(UIImage *)rightImage {
    _rightImage = rightImage;
    _rightTitle = nil;
    [self refreshData];
}

- (void)setMarginLeft:(CGFloat)marginLeft {
    _marginLeft = marginLeft;
    [self refreshData];
}

- (void)setMarginRight:(CGFloat)marginRight {
    _marginRight = marginRight;
    [self refreshData];
}

- (void)setLeftSize:(CGSize)leftSize {
    _leftSize = leftSize;
    [self refreshData];
}

- (void)setRightSize:(CGSize)rightSize {
    _rightSize = rightSize;
    [self refreshData];
}

- (void)setLeftImageName:(NSString *)leftImageName {
    _leftImageName = leftImageName;
    _leftImage = [UIImage imageNamed:leftImageName];
    _leftTitle = nil;
    [self refreshData];
}

- (void)setRightImageName:(NSString *)rightImageName {
    _rightImageName = rightImageName;
    _rightImage = [UIImage imageNamed:rightImageName];
    _rightTitle = nil;
    [self refreshData];
}

@end
